package com.example.dell.ssm.UI;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.ssm.R;
import com.example.dell.ssm.app.Api;
import com.example.dell.ssm.app.RetrofitManager;
import com.jaydenxiao.common.commonutils.LogUtils;
import com.jaydenxiao.common.commonutils.ToastUitl;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {

    @butterknife.Bind(R.id.back_iv)
    ImageView backIv;
    @butterknife.Bind(R.id.message_tv)
    TextView messageTv;
    @butterknife.Bind(R.id.name_ed)
    EditText nameEd;
    @butterknife.Bind(R.id.pwd_ed)
    EditText pwdEd;
    @butterknife.Bind(R.id.btn)
    Button btn;
    @butterknife.Bind(R.id.register_tv)
    TextView registerTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butterknife.ButterKnife.bind(this);
    }

    @butterknife.OnClick({R.id.back_iv, R.id.btn, R.id.register_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                break;
            case R.id.btn:
                if ("".equals(nameEd.getText().toString())) {
                    ToastUitl.showShort("用户名不能为空");
                    return;
                }
                if ("".equals(pwdEd.getText().toString())) {
                    ToastUitl.showShort("密码必能为空");
                    return;
                }
                Api service = RetrofitManager.getInstance().createService(Api.class);
                Call<String> login = null;
                login = service.login(nameEd.getText().toString(), pwdEd.getText().toString());
                LogUtils.loge("request    "+login.request().toString());
                login.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        ToastUitl.showShort(response.body().toString());
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        ToastUitl.showShort("sssssssssssssssssss");
                    }
                });
                break;
            case R.id.register_tv:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
        }
    }
}
